//
//  MBSpaceMyCollectionViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceMyCollectionViewController.h"
#import "MBSpaceMyCollectionShowViewController.h"
#import "MBSongDetailController.h"

#import "MBSpaceMyParentChildShowTabbarView.h"
#import "ScreenTypeView.h"

#define TABBAR_INDEX    (3)

@interface MBSpaceMyCollectionViewController ()<MBSpaceMyParentChildShowTabbarViewDelegate,UIScrollViewDelegate,ScreenTypeViewDelegate> {
    MBSpaceMyParentChildShowTabbarView      *_tabbarView;
    ScreenTypeView                          *_screenTypeView;
    UIScrollView                            *_scrollView;
    NSMutableArray                          *_ViewCtrlArr; //存储控制器
    MBSpaceMyCollectionShowViewController   *_oldShowViewCtrl;
    NSInteger                               selectTabbarIndex;
    
    NSString                                *fenlei;
    NSString                                *fenling;
    NSString                                *changjing;
    UIButton                                *shuaixuanBt;
    MyCollectionShowType                    _detailType;
}

@end

@implementation MBSpaceMyCollectionViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self onUpdateUIForView:self.orientation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    fenlei = @"";
    fenling = @"";
    changjing = @"";
    // Do any additional setup after loading the view.
    [self onSetNavBar];
    [self onCreateUI];
    [self addController];
}

#pragma mark - Actions
#pragma mark - UI
- (void)onSetNavBar
{
    [self setBackItem];
    
    if (_type == MBSpaceMyCollectionShowType_Collection) {
        [self setTitleView:@"收藏"];
        
       [self setRightPlayerItem];
    }
    else  if (_type == MBSpaceMyCollectionShowType_Search) {
        [self setTitleView:@"搜索结果"];
        
        shuaixuanBt = [UIButton buttonWithType:UIButtonTypeCustom];
        shuaixuanBt.frame = CGRectMake(0, 0, 60, 30);
        [shuaixuanBt setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
        [shuaixuanBt setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateHighlighted];
        [shuaixuanBt setImageEdgeInsets:UIEdgeInsetsMake(-40, 0, -40, -40)];
        [shuaixuanBt addTarget:self action:@selector(onSelectItemForSearch:) forControlEvents:UIControlEventTouchUpInside];
        [self setRightBarItems:shuaixuanBt];
    }
}

- (void)onCreateUI
{
    //歌曲、专辑、亲子秀 分栏
    selectTabbarIndex = 0;
    if (_type == MBSpaceMyCollectionShowType_Collection) {
        _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"歌曲",@"专辑",@"亲子秀", nil];
    }
    else if (_type == MBSpaceMyCollectionShowType_Record) {
        _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"歌曲",@"专辑",@"亲子秀", nil];
    }
    else {
        _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"歌曲",@"专辑",@"主播", nil];
    }
    _tabbarView = [[MBSpaceMyParentChildShowTabbarView alloc]initWithFrame:CGRectMake(0, (ISIPAD?NAV_HEIGHT:(ISPortrait?NAV_HEIGHT:36)), SCREEN_WIDTH, 80*PERCEN) withCount:_ViewCtrlArr withbgColor:[UIColor blackColor] withSelectColor:TINK_BG_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];
    
    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, self.view.height-_tabbarView.bottom)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*TABBAR_INDEX, _scrollView.height);
    
    if (_type == MBSpaceMyCollectionShowType_Search) {
        _screenTypeView = [[ScreenTypeView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 0)];
        _screenTypeView.delegate = self;
        _screenTypeView.orientation = self.orientation;
        [self.view addSubview:_screenTypeView];
        [self.view insertSubview:_screenTypeView aboveSubview:_scrollView];
    }
    
    //测试
    [self performSelector:@selector(onRelayForUpdateUI) withObject:nil afterDelay:0];
}

//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        MBSpaceMyCollectionShowViewController *show = [[MBSpaceMyCollectionShowViewController alloc]init];
        if (i == 0) {
            show.type = MyCollectionShowType_Music;
        }
        else if (i == 1) {
            show.type = MyCollectionShowType_Album;
        }
        else if (i == 2) {
            show.type = MyCollectionShowType_ParentChildShow;
        }
        
        show.spaceType = self.type;
        show.searchText = self.searchText;
        show.taUseId = self.taUseId;
        show.fenlei = fenlei;
        show.fenling = fenling;
        show.changjing = changjing;
        
        //是否隐藏table的表头
        if (_type == MBSpaceMyCollectionShowType_Search) {
            show.isHideAllTableHeaderView = YES;
            show.spaceType = MBSpaceMyCollectionShowType_Search;
        }
        else if (_type == MBSpaceMyCollectionShowType_Record) {
            show.isHideAllTableHeaderView = YES;
            show.spaceType = MBSpaceMyCollectionShowType_Record;
        }
        else if (_type == MBSpaceMyCollectionShowType_Collection) {
            show.isHideAllTableHeaderView = NO;
            show.spaceType = MBSpaceMyCollectionShowType_Collection;
        }
    
        [self addChildViewController:show];
        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
            show.onChangeCompileStatus = ^(BOOL compileStatus,MyCollectionShowType type) {
                _detailType = type;
                [self onChangeBackButton:compileStatus];
            };
            show.onBackChangeTitle = ^ (MyCollectionShowType type,NSInteger count) {
                if (type == MyCollectionShowType_Music) {
                    [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"歌曲(%ld)",count] withIndex:0];
                }
                else if (type == MyCollectionShowType_Album) {
                    [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"专辑(%ld)",count] withIndex:1];
                }
                else if (type == MyCollectionShowType_ParentChildShow) {
                    [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"亲子秀(%ld)",count] withIndex:2];
                } else if (type == MyCollectionShowType_Anchor) {
                    [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"主播(%ld)",count] withIndex:2];
                }
            };
            show.onChangeSuperLeftBt = ^ (BOOL isAllSelect) {
                if (isAllSelect == NO) {
                    [self onChangeBackButton:YES];
                }
            };
            [_scrollView addSubview:show.view];
        }
        else if (i == 1) {
            show.onChangeCompileStatus = ^(BOOL compileStatus,MyCollectionShowType type) {
                _detailType = type;
                [self onChangeBackButton:compileStatus];
            };
        }
        else if (i == 2) {
            show.onChangeCompileStatus = ^(BOOL compileStatus,MyCollectionShowType type) {
                _detailType = type;
                [self onChangeBackButton:compileStatus];
            };
            show.onChangeSuperLeftBt = ^ (BOOL isAllSelect) {
                if (isAllSelect == NO) {
                    [self onChangeBackButton:YES];
                }
            };
        }
    }
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tabbarView.frame = CGRectMake(0, (ISIPAD?NAV_HEIGHT:(ISPortrait?NAV_HEIGHT:36)), SCREEN_WIDTH, 80*PERCEN);
    _tabbarView.orientation = self.orientation;
    _scrollView.frame = CGRectMake(0, _tabbarView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-_tabbarView.bottom);
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);
    for (int i=0; i<self.childViewControllers.count; i++) {
        MBSpaceMyCollectionShowViewController *show = (MBSpaceMyCollectionShowViewController *)[self.childViewControllers objectAtIndex:i];
        if ([show isKindOfClass:[MBSpaceMyCollectionShowViewController class]]) {
            show.view.frame = CGRectMake(_scrollView.width*i, 0, _scrollView.width, _scrollView.height);
        }
    }
    [_scrollView setContentOffset:CGPointMake(selectTabbarIndex*_scrollView.width, 0) animated:YES];
}

#pragma mark - 跳转播放器
- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

#pragma mark - 筛选
- (void)onSelectItemForSearch:(UIButton *)bt
{
    bt.selected = !bt.selected;
    _screenTypeView.orientation = self.orientation;
    [_screenTypeView showInView:bt.selected];
}

- (void)onShowScreenTypeWithFenLei:(int)fenle withFenLing:(int)fenlin withChangJiang:(int)changjin withView:(ScreenTypeView *)view
{
    if (fenle == 100) {
        fenlei = @"";
    } else {
        fenlei = [NSString stringWithFormat:@"%d",fenle+1];
    }
    if (fenlin == 100) {
        fenling = @"";
    } else {
        fenling = [NSString stringWithFormat:@"%d",fenlin+1];
    }
    if (changjin == 100) {
        changjing = @"";
    } else {
        changjing = [NSString stringWithFormat:@"%d",changjin+1];
    }
    
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    MBSpaceMyCollectionShowViewController *show = self.childViewControllers[index];
    if ([show isKindOfClass:[MBSpaceMyCollectionShowViewController class]]) {
        show.fenlei = fenlei;
        show.fenling = fenling;
        show.changjing = changjing;
        show.searchText = self.searchText;
        [show onLoadNew];
    }
}

#pragma mark - 改变返回按钮样式
- (void)onChangeBackButton:(BOOL) compileStatus {
    if (compileStatus) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 60, 30);
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14.]];
        [btn setTitle:@"全选" forState:UIControlStateNormal];
        [btn setTitle:@"全选" forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
        [btn addTarget:self action:@selector(onSelectAllMusicAction:) forControlEvents:UIControlEventTouchUpInside];

        [self setLeftBarItem:btn];
        [self setRightBarItems:nil];
    } else {
        [self setBackItem];
        if (self.type == MBSpaceMyCollectionShowType_Collection) {
            [self setRightPlayerItem];
        }
    }
}

//全选
- (void)onSelectAllMusicAction:(UIButton *)bt
{
    MBSpaceMyCollectionShowViewController *show = self.childViewControllers[(_detailType == MyCollectionShowType_Music?0:2)];
    if ([show isKindOfClass:[MBSpaceMyCollectionShowViewController class]]) {
        bt.selected = !bt.selected;
        if (bt.selected) {
            [bt setTitle:@"全不选" forState:UIControlStateNormal];
            [bt setTitle:@"全不选" forState:UIControlStateHighlighted];
            [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 15)];
        } else {
            [bt setTitle:@"全选" forState:UIControlStateNormal];
            [bt setTitle:@"全选" forState:UIControlStateHighlighted];
            [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 25)];
        }
        [show onSelectAllMusicAction:bt.selected];
    }
}

//更新Tabbar
- (void)onRelayForUpdateUI
{
    [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"歌曲(%d)",(int)[UserManager standerDefult].collectionMusicArr.count] withIndex:0];
    if (_type == MBSpaceMyCollectionShowType_Collection) {
        [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"专辑(%d)",(int)[UserManager standerDefult].collectionAlbumArr.count] withIndex:1];
        [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"亲子秀(%d)",(int)[UserManager standerDefult].collectionShowArr.count] withIndex:2];
    }
    else if (_type == MBSpaceMyCollectionShowType_Search) {
        [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"专辑(0)"] withIndex:1];
        [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"主播(0)"] withIndex:2];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 分栏选择器 歌曲、专辑、亲子秀
- (void)onSelectItemForView:(MBSpaceMyParentChildShowTabbarView *)view withIndex:(NSInteger)selectIndex
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectIndex) {
        selectTabbarIndex = selectIndex;
        [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
    }
}

#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (self.type == MBSpaceMyCollectionShowType_Search) {
        //主播屏蔽shaixuan
        if (index == 2) {
            shuaixuanBt.hidden = YES;
        } else {
            shuaixuanBt.hidden = NO;
        }
    }
    
    MBSpaceMyCollectionShowViewController *show = self.childViewControllers[index];
    show.fenlei = fenlei;
    show.fenling = fenling;
    show.changjing = changjing;
    show.searchText = self.searchText;
    show.onBackChangeTitle = ^ (MyCollectionShowType type,NSInteger count) {
        if (type == MyCollectionShowType_Music) {
            [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"歌曲(%ld)",count] withIndex:0];
        }
        else if (type == MyCollectionShowType_Album) {
            [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"专辑(%ld)",count] withIndex:1];
        }
        else if (type == MyCollectionShowType_Anchor) {
            if (_type == MBSpaceMyCollectionShowType_Collection) {
                [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"亲子秀(%ld)",count] withIndex:2];
            } else if (_type == MBSpaceMyCollectionShowType_Search) {
                [_tabbarView onUpdateUIForTitle:[NSString stringWithFormat:@"主播(%ld)",count] withIndex:2];
            }
        }
    };
    [show onUpdateUIForSelectScrollView];
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}

@end
